package com.songqintuan.platform.tree;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import org.richfaces.model.TreeNode;

public class ResourceTreeNode implements TreeNode {
    
	private String id;//对应entity id
	private String name;
	private boolean checkboxEnabled = false;   //节点前复选框是否可选
	private boolean selected = false; 
	/* tree node impl */
	private List<Object> keysList = null;
    private Map<Object, TreeNode> children = null;
    private boolean leaf;
    /* end */
	
	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public boolean isCheckboxEnabled() {
		return checkboxEnabled;
	}


	public void setCheckboxEnabled(boolean checkboxEnabled) {
		this.checkboxEnabled = checkboxEnabled;
	}


	public boolean isSelected() {
		return selected;
	}


	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	/* tree node impl */
	public ResourceTreeNode() {
        this(false);
    }

    public ResourceTreeNode(boolean leaf) {
        super();

        this.leaf = leaf;

        if (!leaf) {
            keysList = Lists.newArrayList();
            children = Maps.newHashMap();
        }
    }
	
	public void addChild(Object key, TreeNode child) {
        if (isLeaf()) {
            throw new IllegalStateException("Cannot add children to leaf");
        }

        keysList.add(key);
        children.put(key, child);
    }

    public void insertChild(int idx, Object key, TreeNode child) {
        if (isLeaf()) {
            throw new IllegalStateException("Cannot add children to leaf");
        }

        keysList.add(idx, key);
        children.put(key, child);
    }

    public void removeChild(Object key) {
        if (isLeaf()) {
            return;
        }

        children.remove(key);
        keysList.remove(key);
    }

    public TreeNode getChild(Object key) {
        if (isLeaf()) {
            return null;
        }

        return children.get(key);
    }

    public Iterator<Object> getChildrenKeysIterator() {
        if (isLeaf()) {
            return Iterators.emptyIterator();
        }

        return Iterators.unmodifiableIterator(keysList.iterator());
    }

    public int indexOf(Object key) {
        if (isLeaf()) {
            return -1;
        }

        return keysList.indexOf(key);
    }

    public boolean isLeaf() {
        return leaf;
    }
    
	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}
	/* end */




	public ResourceTreeNode(Long id, String name, boolean leaf, boolean checkboxEnabled, boolean selected ){
		super();
		this.id = String.valueOf(id);
		this.name = name;
		this.leaf = leaf;
		this.checkboxEnabled = checkboxEnabled;
		this.selected = selected;
		if (!leaf) {
            keysList = Lists.newArrayList();
            children = Maps.newHashMap();
        }
	}
	
	public ResourceTreeNode(Long id, String name, boolean leaf, boolean checkboxEnabled, boolean selected, TreeNode parent ){
		super();
		this.id = String.valueOf(id);
		this.name = name;
		this.leaf = leaf;
		this.checkboxEnabled = checkboxEnabled;
		this.selected = selected;
		if (!leaf) {
            keysList = Lists.newArrayList();
            children = Maps.newHashMap();
        }
		parent.addChild(String.valueOf(id), this);
	}
}

